package app.service.message;

import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import app.service.message.ConfigurationConstants;
import app.service.message.GroupTravellingMessaging;
import android.util.Log;

import com.google.gson.Gson;

public class GroupTravellingClient {
	GroupTravellingMessaging gtm;
	private static final String URL = ConfigurationConstants.SERVER_URL
			+ ConfigurationConstants.GROUP_TRAVELLING_IM_SERVLET;

	public GroupTravellingClient(GroupTravellingMessaging gtm) {
		this.gtm = gtm;
		execute();
	}

	/*
	 * @see no.hiof.edu.services.Client#execute()
	 */
	public void execute() {
		Log.d("Instant Messenger Client", "In Execute");
		try{
		URL urlObject = new URL(URL);
		URLConnection conn = urlObject.openConnection();
		HttpURLConnection httpConn = (HttpURLConnection) conn;
		httpConn.setRequestMethod("POST");
		httpConn.setDoInput(true);
		httpConn.setDoOutput(true);
		httpConn.connect();

		DataOutputStream dataStream = new DataOutputStream(conn
				.getOutputStream());
		String json = new Gson().toJson(gtm);
		dataStream.writeBytes(json);
		dataStream.flush();
		dataStream.close();
		int responseCode = httpConn.getResponseCode();
		if (responseCode == HttpURLConnection.HTTP_OK) {
			Log.d("Request sent","Response Received");
		}
		} catch(Exception e){
			System.out.println("Exception in execute in AuthenticationClient::"+e.getMessage());
		}
	}
}
